﻿<script>
	var pub_ngapp = angular.module("ngapp", []);

	var pub_scope;
	pub_ngapp.controller('cashbook_add', function ($scope, $filter)
	{
		func_page_init($scope, $filter);

		//var d = $filter("date")("2015-02-21", "yyyy-MM-dd") // yyyy/MM/dd指定转换的格式
	});
</script>


<script>
	/////////////////////////////////
	////页面初始化API
	function func_page_init(scope, filter)
	{
		var url = 'api__api.aspx';

		$.ajax({
			dataType: "json",
			url: url,
			data:
			    {
			    	__type: "func_page__cashbook__add",
			    	
			    	id: pub_id,
			    	
			    	__d: (new Date()).valueOf()
			    },
			//////////////////////////////////////////////////
			success: function (data, textStatus, jqXHR)
			{
				setTimeout(function ()
				{
					pub_scope = scope;
					pub_scope.$apply(function ()
					{
						func_page_init__success(data, filter);
					});
				}, 100);
			},
			//////////////////////////////////////////////////
			error: function (data, textStatus, jqXHR)
			{
			}
		});
	}


	function func_page_init__success(data, filter)
	{
		console.log(pub_scope);
		console.log(filter);


		pub_scope.cash_date = new Date();
		pub_scope.type = "";
		pub_scope.ab_type = "-";
		pub_scope.num = ""
		pub_scope.desc = "";


		var entity_cashbook = data.page.entity_cashbook;
		if (entity_cashbook)
		{
			pub_scope.cash_date = entity_cashbook.cashrec_date.split(' ')[0];
			pub_scope.type = entity_cashbook.cashrec_type;
			pub_scope.ab_type = entity_cashbook.cashrec_ab_type;

			pub_scope.num = entity_cashbook.cashrec_num;

			pub_scope.desc = entity_cashbook.cashrec_desc;
			
			
			$(".ab_type").val(pub_scope.ab_type);
			$("#compNum").val(pub_scope.num);
			$("#compDesc").val(pub_scope.desc);
			
			
			$(".type div").each(function()
			{
				var class_name='select';
				
				//alert($(this).attr("data-type"));
				if ($(this).attr("data-type")== pub_scope.type ) 
				{
					$(this).addClass(class_name);
				} 
			});
		}
	}
</script>


<script>
	$(function ()
	{
		$(".btn-success").click(function ()
		{
			var date = $(".date").text();
			var desc = $("#compDesc").val();
			var ab_type = $(".ab_type").val();

			var type = $(".type .select").attr("data-type");
			var num = $("#compNum").val();


			if (typeof (type) == "undefined")
			{
				func_alert("请选择类型");
				return;
			}

			if (num.length <= 0)
			{
				func_alert("请填写金额");
				return;
			}


			func_cashbook_add(date, type, ab_type, num, desc);  
		});
	});
</script>

<script>
function func_cashbook_add__success(data, textStatus, jqXHR) 
{
	console.log(data);
	console.log(textStatus);
	console.log(jqXHR);

	if (data.errcode == 0) 
	{
		location.href = "cashbook__index.aspx";
	} 
	else
	{
		func_alert(data.errmsg);
	}
}
</script>


<script>
	$(function() {
		$(".type div").click(function() {
			var class_name='select';
			
			
			if (!$(this).hasClass(class_name)) 
			{
				//alert($(this).attr("data-type"));
				
				$(".type div").removeClass(class_name);
				$(this).addClass(class_name);
			} 
			else
			{
				$(this).removeClass(class_name);
			}
		});
	})
</script>


<script>
    $(function ()
    {
        $(".date").mobiscroll().date({
            theme: "android-ics light",
            lang: "zh",
            cancelText: null,
            dateFormat: 'yy-mm-dd',
            animate: "flip",
            headerText: function (valueText)
            {
                //自定义弹出框头部格式  
                array = valueText.split('-');
                var date_str = array[0] + "年" + array[1] + "月" + array[2] + "日";
                $(".date").html(valueText);
                
                pub_scope.cash_date = new Date(valueText);
                
                return date_str;
            }
        });
    })
</script>